aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/payment/[id]/+page.svelte
blob: 3fae32dce28c077d7531a9d3a3f0f9de17387de5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<script>
  import { browser } from "$app/environment";
  import { onMount, tick } from "svelte";
  import { t, loading } from "$lib/translations";
  import {
    copy,
    f,
    s,
    loc,
    toFiat,
    post,
    sats,
    fail,
    success,
    types,
  } from "$lib/utils";
  import Avatar from "$comp/Avatar.svelte";
  import Icon from "$comp/Icon.svelte";
  import { format } from "date-fns";
  import { PUBLIC_EXPLORER, PUBLIC_LIQUID_EXPLORER } from "$env/static/public";
  import locales from "$lib/locales";
  import { goto } from "$app/navigation";

  let { data } = $props();
  let { user, payment: p } = $state(data);
  let locale = user ? locales[user.language] : locales["en"];
  let userLocale = loc(user);

  let {
    id,
    hash,
    amount,
    created,
    confirmed,
    memo,
    rate,
    type,
    ref,
    path,
    tip,
    ourfee,
    fee = 0,
    currency,
  } = $derived(p);
  let [txid, vout] = $derived(amount > 0 ? ref.split(":") : [hash]);
  let a = $derived(Math.abs(amount));

  let expl = $derived(
    {
      bitcoin: PUBLIC_EXPLORER,
      liquid: PUBLIC_LIQUID_EXPLORER,
    }[type],
  );

  let print = async () => {
    await post(`/payment/${id}/print`, { id });
    success("Printing!");
  };

  let bump = async () => {
    try {
      await post(`/payment/${id}/bump`, { id });
    } catch (e) {
      fail(e.message);
    }
  };

  let direction = $state("");
  onMount(() => {
    direction = amount > 0 ? $t("payments.from") : $t("payments.to");

    if (direction)
      direction =
        direction[0].toUpperCase() + direction.substr(1, direction.length);
  });
</script>

{#snippet field(title, amount)}
  <div>
    <span class="text-lg text-secondary">{$t(title)}</span>
    <div class="flex gap-2 items-end">
      <div class="flex items-center">
        <iconify-icon noobserver icon="ph:lightning-fill" class="text-yellow-300"
        ></iconify-icon>{s(amount, userLocale)}
      </div>
      <span class="text-secondary text-lg">
        {f(toFiat(amount, rate), currency, userLocale)}
      </span>
    </div>
  </div>
{/snippet}

<div class="container mx-auto max-w-lg px-4 space-y-8 break-all text-2xl">
  <h1 class="px-3 md:px-0 text-center text-3xl md:text-4xl font-semibold mb-10">
    {$t(amount < 0 ? "payments.sent" : "payments.received")}
  </h1>

  {#if p.with}
    <a href={`/${p.with.username}`}>
      <span class="text-lg text-secondary my-auto mr-2">{direction}</span>
      <div class="flex">
        <div class="my-auto">
          <Avatar user={p.with} size={20} disabled={true} />
        </div>
        <div class="my-auto ml-1">{p.with.username}</div>
      </div>
    </a>
  {/if}
  {#if p.type === types.fund}
    <div>
      <span class="text-lg text-secondary my-auto mr-2">{direction}</span>
      <a href={`/fund/${p.memo}`}>
        <div class="flex">
          <div class="my-auto">
            <iconify-icon noobserver
              icon="ph:lightning-fill"
              width="24"
              class="text-yellow-300"
            ></iconify-icon>
          </div>
          <div class="my-auto ml-1">{p.memo}</div>
        </div>
      </a>
    </div>
  {/if}

  {@render field("payments.amount", a)}

  {#if tip}
    {@render field("invoice.tip", tip)}
    {@render field("payments.total", a + (tip || 0))}
  {/if}

  {@render field("payments.networkFee", fee)}

  {#if ourfee > 0}
    {@render field("payments.platformFee", ourfee)}
  {/if}

  <div>
    <span class="text-lg text-secondary">{$t("payments.date")}</span>
    <div>
      {format(new Date(created), "h:mmaaa", { locale })}
      {format(new Date(created), "MMM d, yyyy", { locale })}
    </div>
  </div>

  {#if type === types.ecash && amount < 0}
    <a href={`/ecash/${memo}`} class="block">
      <button class="btn">
        <img src="/images/cash.png" class="w-8" alt="Cash" />
        <div>{$t("payments.ecashToken")}</div>
      </button>
    </a>
  {:else if memo}
    <div>
      <span class="text-lg text-secondary">{$t("payments.memo")}</span>
      <div>{memo}</div>
    </div>
  {/if}

  {#if type === types.lightning || type === types.bolt12}
    <div>
      <span class="text-lg text-secondary">{$t("payments.preimage")}</span>
      <div>{ref}</div>
    </div>
  {/if}

  {#if type === "bitcoin" || type === "liquid"}
    <div>
      <span class="text-lg text-secondary">Txid</span>
      <div class="flex">
        <div>
          <a
            href={`${expl}/tx/${txid}${vout ? "#vout=" + vout : ""}`}
            target="_blank"
            rel="noreferrer">{txid}</a
          >
        </div>
        <button
          class="flex font-bold hover:opacity-80 mb-auto my-auto"
          onclick={() => copy(txid)}
        >
          <Icon icon="copy" style="ml-2 w-12 my-auto" />
        </button>
      </div>
    </div>
  {/if}

  {#if path}
    <div>
      <span class="text-lg text-secondary">Path</span>
      <div class="flex">
        <div>{path}</div>
        <button
          class="flex font-bold hover:opacity-80 mb-auto my-auto"
          onclick={() => copy(path)}
        >
          <Icon icon="copy" style="ml-2 w-12 my-auto" />
        </button>
      </div>
    </div>
  {/if}
</div>